var mainLayout;
var innerlayout;
var toolBarHeight = 29;
function fullGridEdit(gridId) {
	try {
		var ids = jQuery(gridId).jqGrid('getDataIDs');
		for ( var i = 0; i < ids.length; i++) {
			var cl = ids[i];
			jQuery(gridId).editRow(cl);
		}
	} catch (e) {
		alert(e.name + " :  " + e.message);
	}
}
function restoreRowGrid(gridId) {
	try {
		var ids = jQuery(gridId).jqGrid('getDataIDs');
		for ( var i = 0; i < ids.length; i++) {
			var cl = ids[i];
			jQuery(gridId).restoreRow(cl);
		}
	} catch (e) {
		alert(e.name + " :  " + e.message);
	}
}
function saveRowGrid(gridId, saveUrl) {
	try {

		var ids = jQuery(gridId).jqGrid('getDataIDs');
		for ( var i = 0; i < ids.length; i++) {
			var cl = ids[i];
			if (jQuery(jQuery(gridId).jqGrid("getInd", cl, true)).attr(
					"editable") === "1") {
				// the row having id=rowid is in editing mode

				var returnRes = jQuery(gridId).saveRow(cl, {
					successfunc : function(response) {
						alert("successfunc");
						return true;
					},
					url : saveUrl,
					aftersavefunc : function(response) {
						alert("aftersavefunc");
						return true;
					},
					errorfunc : function(response) {
						alert("errorfunc");
						return true;

					},
					restoreAfterError : false
				});

				if (returnRes) {
					alert("return is: " + returnRes);
					continue;
				} else {
					break;
				}
			}
		}
		// var rowsData = jQuery(gridId).jqGrid('getRowData');
		// alert(rowsData);
	} catch (err) {
		alert(err);
	}
}
function resizeGrid(gridid, pane_state,pane_selector) {
	if (jQuery.browser.msie) {
		if (jQuery.browser.version < 9) {
			var iw = 2, ih = 71;
		} else {
			var iw = 2, ih = 74;
		}
	} else if (jQuery.browser.opera) {
		var iw = 2, ih = 73;
	} else if (jQuery.browser.mozilla) {
		var iw = 2, ih = 75;
	} else {
		var iw = 7, ih = 77;
	}
	var headerHeight = jQuery(pane_selector+">.header").height()+5;
	//alert(headerHeight);
	if(headerHeight==20)
		headerHeight=0;
	jQuery(gridid).jqGrid('setGridWidth', pane_state.innerWidth - iw - 1,		'true');
	jQuery(gridid).jqGrid('setGridHeight', pane_state.innerHeight - ih - 1-headerHeight,				'true');// 50,75
}
function initToolBar(resizerClass, toolbarHtmlString, toolbarInitMethod) {
	jQuery(resizerClass).append(toolbarHtmlString);
	jQuery(toolbarInitMethod);
}
function floatInformation(message){
	jQuery.jGrowl(message);
}
//TODO 需要处理提示信息问题
function deleteRecords(delUrl,gridID) {
	var sid = jQuery("#" + gridID).jqGrid('getGridParam', 'selarrrow');
	if (sid == null || sid.length == 0) {
		return promptMsg("提示", "请至少选择一条记录");
	} else {
		confirmMsg("确认","确认删除所选择记录吗？",function(){
			var url = delUrl+"?delIds="+sid;
			// jQuery("#loading").show();
			 var jqxhr=jQuery.post( url, {dataType : "json"}).success(function(data){
		      	var status = data['returnCode'];
		    	if(status===300){
					alert(data['message']);
					promptMsg("提示", data['message']);
		    	}else{
		    		refreshGrid(gridID, true);
		    		floatInformation(data['message']);
		    	}
			
				});
			}
			
		);

	}
}

function deleteSingelRecord(delUrl,gridID){
	var sid = jQuery("#" + gridID).jqGrid('getGridParam', 'selrow');
	if (sid == null) {
		return promptMsg("提示", "请至少选择一条记录");
	} else {
		confirmMsg("确认","确认删除所选择记录吗？",function(){
			var url = delUrl+"?delIds="+sid;
			// jQuery("#loading").show();
			 var jqxhr=jQuery.post( url, {dataType : "json"}).success(function(data){
		      	var status = data['returnCode'];
		    	if(status===300){
					//alert(data['message']);
					promptMsg("提示", data['message']);
		    	}else{
		    		refreshGrid(gridID, true);
		    		floatInformation(data['message']);
		    	}
			
				});
			}
			
		);

	}
}

function otherActionCall(url,gridID,isRefreshGrid){
	try{
	 var jqxhr=jQuery.post( url, {dataType : "json"}).success(function(data){
	      	var status = data['returnCode'];
	    	if(status===300){
				//alert(data['message']);
				promptMsg("提示", data['message']);
	    	}else{
	    		if(isRefreshGrid)
	    			refreshGrid(gridID, false);
	    		floatInformation(data['message']);
	    	}
		
			});
	}catch(e){
		promptMsg("提示", e.message);
	}
}


function confirmMsg(dialogTitle, msg, callBack) {
	jQuery("#dialog-confirm>#msg>b").html(msg);
	jQuery("#dialog-confirm").dialog({
		resizable : false,
		title : dialogTitle,
		// height:140,
		// zIndex: 10000,
		modal : false,
		buttons : {
			Ok : function() {
				jQuery(this).dialog("close");
				callBack();
			},
			Cancel : function() {
				jQuery(this).dialog("close");
			}
		}
	});
}

function promptMsg(dialogTitle, msg) {
	jQuery("#dialog-message>#msg>b").html(msg);
	jQuery("#dialog-message").dialog({
		title : dialogTitle,
		modal : true,
		zIndex : 10000,
		buttons : {
			Ok : function() {
				jQuery(this).dialog("close");
			}
		}
	});
}
function addNewRecord(baseUrl, gridID, width, height) {
	popUpForm(baseUrl, gridID, '', '', '', width, height);
}
function editRecord(baseUrl, gridID, modelIdName, width, height) {
	popUpForm(baseUrl, gridID, modelIdName, '', '', width, height);
}
function addNewChildRecord(baseUrl, gridID, fatherGridID, fatherIdName, width,
		height) {
	popUpForm(baseUrl, gridID, '', fatherGridID, fatherIdName, width, height);
}
function editChildRecord(baseUrl, gridID, modelIdName, fatherGridID,
		fatherIdName, width, height) {
	popUpForm(baseUrl, gridID, modelIdName, fatherGridID, fatherIdName, width,
			height);
}
function popUpForm(baseUrl, gridID, modelIdName, fatherGridID, fatherIdName,
		width, height) {
	if (baseUrl === null || baseUrl === '') {
		alert("baseUrl is required!");
		return;
	}
	if (gridID === null || gridID === '') {
		alert("gridID is required!");
		return;
	}

	var modelIdValue;
	if (modelIdName != null && modelIdName != '')
		modelIdValue = jQuery("#" + gridID).jqGrid('getGridParam', 'selrow');

	var fatherIdValue;
	if (fatherIdName != null && fatherIdName != '')
		fatherIdValue = jQuery("#" + fatherGridID).jqGrid('getGridParam',
				'selrow');

	var url = baseUrl + "?gridId=" + gridID;
	if (modelIdName != null && modelIdName != '')
		url = url + "&" + modelIdName + "=" + modelIdValue;
	if (fatherIdName != null && fatherIdName != '')
		url = url + "&" + fatherIdName + "=" + fatherIdValue;

	// alert(url);
	jQuery.colorbox({
		href : url,
		iframe : true,
		width : width,
		height : height
	});
}

function commonPopUp(url,title,width, height){
	jQuery.colorbox({
		href : url,
		iframe : true,
		width : width,
		height : height
	});
}
function refreshGrid(gridID, isCurrentPage) {
	var jq = jQuery('#' + gridID);
	// alert(jq);
	var currentPage;
	if (isCurrentPage)
		currentPage = jq.jqGrid('getGridParam', 'page');
	else
		currentPage = 1;
	jq.trigger('reloadGrid', [ {
		page : currentPage
	} ]);
}
// jQuery("button").button();

/*------------------------------------------------------------*/

var datePickerOptions = {
	// showOn: "button",
	// buttonImage: "images/calendar.gif",
	// buttonImageOnly: true,
	//dateFormat : '<s:text name="date.format.jqueryui" />'
		dateFormat : 'yy-mm-dd'
};

var formOptions = {
		dataType : "json",
		beforeSubmit : processBeforeSubmit, // pre-submit callback
		success : processResponseAfterSubmit//,
	};
function processResponseAfterSubmit(data,status,xhr,jqform) {
	try {
		if (data.returnCode === 200) {
			parent.refreshGrid(data.gridId, true);
			parent.floatInformation(data.message);
			parent.jQuery.colorbox.close();
		} else {
			
			if ( data.fieldErrors != null && data.fieldErrors.length !== 0 ) {
				$.each(data.fieldErrors, function(e) {
					$("[name$='"+e+"']").validationEngine('showPrompt', data.fieldErrors[e], 'error','centerRight', true);
				});
			}else{
				parent.promptMsg("错误提示", data.message);
			}
		}
	} catch (e) {
		alert(e.message);
	}
}
function processBeforeSubmit(formData, jqForm, options) {
	var v = jqForm.validationEngine('validate');
	if (v) {
		return true
	} else {
		return false;
	}
}
function closePopUpForm(){
	parent.jQuery.colorbox.close();
}
function clear_input_elements(containId) {
    $("#"+containId).find(':input').each(function() {
        switch(this.type) {
            case 'password':
            case 'select-multiple':
            case 'select-one':
            case 'text':
            case 'textarea':
                $(this).val('');
                break;
            case 'checkbox':
            case 'radio':
                this.checked = false;
        }
    });
}